# -*- coding: utf-8 -*-
"""
支持序列化和反序列化
这个实现支持使用 pickle 模块进行序列化和反序列化,确保单例对象在序列化和反序列化后仍然是同一个对象。
"""

import pickle
import threading


class Singleton(object):
    _instance = None
    _lock = threading.Lock()

    def __new__(cls, *args, **kwargs):
        with cls._lock:
            if cls._instance is None:
                cls._instance = super(Singleton, cls).__new__(cls)
        return cls._instance

    def __getstate__(self):
        return self.__dict__

    def __setstate__(self, state):
        self.__dict__.update(state)